<template>
    <div class="login-wrap">
        <div class="ms-login">
            <div class="ms-title">在线培训考试系统</div>
            <el-form :model="param" :rules="rules" ref="login" label-width="0px" class="ms-content">
                <el-form-item prop="useraccount">
                    <el-input v-model="param.useraccount" placeholder="请输入账号">
                        <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="user_name">
                    <el-input v-model="param.user_name" placeholder="请输入姓名">
                        <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" placeholder="请输入密码" v-model="param.password">
                        <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
                    </el-input>
                </el-form-item>

                <el-form-item prop="password2">
                    <el-input type="password" placeholder="请确认密码" v-model="param.password2">
                        <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
                    </el-input>
                </el-form-item>

                <el-form-item>
                    <el-select v-model="param.role" placeholder="请选择注册角色">
                        <el-option label="普通用户" value="consumer"></el-option>
                        <el-option label="管理员" value="admin"></el-option>
                    </el-select>
                </el-form-item>

                <div class="login-btn">
                    <div>
                        <el-button type="primary" @click="submitForm()">注册</el-button>
                    </div>
                    <router-link :to="{ path: '/login' }">
                        <el-button type="warning">去登录</el-button>
                    </router-link>
                </div>
                <p class="login-tips">Tips: 未注册账号</p>
            </el-form>
        </div>
    </div>
</template>

<script>
import { register, registerAdmin } from '../../api/index';
import moment from 'moment';
export default {
    data: function () {
        return {
            param: {
                useraccount: '',
                password: '',
                password2: '',
                role: '',
                user_name: ''
            },
            rules: {
                useraccount: [{ required: true, message: '请输入账号', trigger: 'blur' }],
                password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
                password2: [{ required: true, message: '请输入确认密码', trigger: 'blur' }],
            },
        };
    },
    methods: {
        submitForm() {
            this.$refs.login.validate(valid => {
                // 记录输入的值
                let pwd = this.param.password;
                let pwd2 = this.param.password2;
                let user_name = this.param.user_name;
                let time = moment((new Date().getTime())).format("YYYY-MM-DD hh:mm:ss");
                // console.log('1.0',{time})
                if (pwd == pwd2) {
                    let role = this.param.role;
                    if (role == "admin") {
                        let admin_acc = this.param.useraccount;
                        // 调用注册接口
                        registerAdmin({ admin_acc, admin_pwd: pwd, admin_name: user_name ,admin_create_time:time}).then(content => {
                            let { code, msg, token } = content;
                            if (code == 200) {
                                // 缓存用户令牌
                                localStorage.setItem('auto_token', token);
                                // 缓存用户账号
                                // localStorage.setItem('ms_username', this.param.useraccount);
                                // 弹窗提示
                                this.$message({
                                    message: msg,
                                    type: 'success'
                                });
                                // 跳转主页
                                this.$router.push('/login');
                            }
                            else {
                                // 弹窗提示
                                this.$message({
                                    message: msg,
                                    type: 'warning'
                                });
                            }
                        })
                    } else {
                        // 调用注册接口
                        let user_acc = this.param.useraccount;
                        register({ user_acc, user_pwd: pwd, user_name,user_create_time:time}).then(content => {
                            let { code, msg, token } = content;
                            if (code == 200) {
                                // 缓存用户令牌
                                localStorage.setItem('auto_token', token);
                                // 缓存用户账号
                                // localStorage.setItem('ms_username', this.param.useraccount);
                                // 弹窗提示
                                this.$message({
                                    message: msg,
                                    type: 'success'
                                });
                                // 跳转主页
                                this.$router.push('/login');
                            }
                            else {
                                // 弹窗提示
                                this.$message({
                                    message: msg,
                                    type: 'warning'
                                });
                            }
                        })
                    }
                } else {
                    // 弹窗提示
                    this.$message({
                        message: "两次输入的密码不一致。",
                        type: 'warning'
                    });
                }

            });
        },
    },
};
</script>

<style scoped>
.login-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-image: url(../../assets/img/login-bg.jpg);
    background-size: 100%;
}

.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: #fff;
    border-bottom: 1px solid #ddd;
}

.ms-login {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);
    overflow: hidden;
}

.ms-content {
    padding: 30px 30px;
}

.login-btn {
    text-align: center;
    width: 100%;
    display: flex;
}

.login-btn div,
.login-btn a {
    width: 50%;
    height: 36px;
    margin-bottom: 10px;
    padding: 0 5px;
}

.login-btn a {
    display: block;
}

.login-btn div button,
.login-btn a button {
    width: 100%;
    height: 36px;
}

.login-tips {
    font-size: 12px;
    line-height: 30px;
    color: #fff;
}
</style>